package com.fieldchain.ex.fa100.utils.chart.mk_chart;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Handler;
import android.util.AttributeSet;

import com.fieldchain.ex.fa100.utils.chart.auxiliary.HighlightCombinedRenderer;
import com.fieldchain.ex.fa100.utils.chart.auxiliary.axis.MyXAxis;
import com.fieldchain.ex.fa100.utils.chart.auxiliary.MyXAxisRenderer;
import com.fieldchain.ex.fa100.utils.chart.dataset.MyCandleDataSet;
import com.fieldchain.ex.fa100.utils.chart.utils.data.DataParseCandle;
import com.fieldchain.ex.fa100.utils.chart.utils.render.RenderUtils;
import com.fieldchain.ex.fa100.utils.screen.Dputils;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/11
 */
public class MyCandleChart extends CombinedChart {
    private MyCandleDataSet candleSet;
    private float scaleTag = 1;
    private Context mContext;
    private DataParseCandle dataParse = new DataParseCandle();
    private boolean isFirst = true;

    public MyCandleChart(Context context) {
        super(context);
        mContext = context;
        setChart();

    }

    public MyCandleChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setChart();
    }


    public void setChart() {
        getXAxis().setEnabled(true);
        getXAxis().setDrawAxisLine(false);
        getXAxis().setDrawGridLines(false);
        getXAxis().setAvoidFirstLastClipping(true);
        getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        getAxisLeft().setDrawGridLines(false);
        getAxisLeft().setDrawAxisLine(false);
        getAxisLeft().setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        getAxisLeft().setTextColor(Color.parseColor("#00ffffff"));

        getAxisRight().setTextColor(Color.parseColor("#FFB7C2CE"));
        getAxisRight().setGridColor(Color.parseColor("#33B7C2CE"));
        getAxisRight().setEnabled(true);
        getAxisRight().setDrawGridLines(true);
        getAxisRight().setDrawAxisLine(false);
        getAxisRight().setTextSize(9);
        getAxisRight().enableGridDashedLine(5f, 10f, 0f);
//        getAxisRight().setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);


        setAutoScaleMinMaxEnabled(true);
        setScaleEnabled(false);
        setScaleXEnabled(true);
        setDrawBorders(false);
        setDoubleTapToZoomEnabled(false);
        getLegend().setEnabled(false);

        setMinOffset(0);
        setDescription(null);
        setDragDecelerationEnabled(false);
        setViewPortOffsets(0, 0, Dputils.dp2px(38),  Dputils.dp2px(15));
        setRenderer(new HighlightCombinedRenderer(this, getAnimator(), getViewPortHandler(), Dputils.dp2px( 8)));
    }


    public void setValues(final List<List<String>> list, int type) {
        if (list.size() > 120) {
            scaleTag = (list.size() / 120f);
        }

        CombinedData combinedData = dataParse.getCandData(list, type);
        candleSet = (MyCandleDataSet) combinedData.getCandleData().getDataSets().get(0);
        setData(combinedData);
        RenderUtils.notifyXy(combinedData.getLineData(), list, getXAxis(), getAxisRight());


        if (isFirst) {
            isFirst = false;
            ViewPortHandler viewPortHandlerBar = getViewPortHandler();
            viewPortHandlerBar.setMaximumScaleX(scaleTag * 2);
            viewPortHandlerBar.setMinimumScaleX(scaleTag / 2);
            Matrix touchmatrix = viewPortHandlerBar.getMatrixTouch();
            touchmatrix.postScale(scaleTag, 1f);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getXAxis().setAxisMinimum(0.5f);
                getXAxis().setAxisMaximum((float) list.size()+0.5f);
                moveViewToX(list.size() - 1);
            }
        }, 300);

    }


    @Override
    protected void init() {
        super.init();
        mXAxis = new MyXAxis();
        mXAxisRenderer = new MyXAxisRenderer(mViewPortHandler, getXAxis(), mLeftAxisTransformer, this);

    }

    @Override
    public MyXAxis getXAxis() {
        return (MyXAxis) super.getXAxis();
    }

    public void showHight() {
        if (candleSet != null) candleSet.setHighlightEnabled(true);
    }

    public void hidnHight() {
        if (candleSet != null) candleSet.setHighlightEnabled(false);
    }


}

